<?php
namespace app\index\model;
use think\Model;
use think\Session;
class Filters extends Model
{
	//测试筛选
    protected $autoWriteTimestamp = 'timestamp';
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';
    protected $table = 'caravan_calendar';
    use \app\common\traits\model\TraitModel;
    const CALENDAR_IS_SHARE = 1;//分享
    const CALENDAR_IS_RENT = 2;//出租
    /**
     * [caravans 关联]
     * @return [type] [description]
     */
    public function caravans()
    {
        return $this->belongsTo('CaravanInfos','caravan_id');
    }
    public function camps()
    {
        return $this->belongsTo('Camps','camp_id');
    }
    public function rents()
    {
        return $this->hasMany('Calendar','caravan_id','caravan_id');
    }
    ////////////////////////////sign = 1 类型判断房车与出租//////////////////////////
    /**
     * [getList1  房车列表1同一城市]
     * @param  [type] $filter_info [description]
     * @param  [type] $page        [description]
     * @return [type]              [description]
     */
    public function getList1($filter_info,$page)
    {
        //时间非空
        if (!isset($filter_info['sdate']) || !isset($filter_info['edate'])) {
            return false;
        }
        //当前时间资源开始时间需大于现在时间
        $now_time =date('Y-m-d');
        //剔除自己的房车
        $user_id = session('user.id');
        $car_id = model('CaravanInfos')->where('user_id',$user_id)->value('id');
        //地点判断区域
        if (!empty($filter_info['zone'])) {
            return $this->with('camps,caravans')->field('*,TIMESTAMPDIFF(DAY, (case when e > "'.$filter_info['edate'].'" then "'.$filter_info['edate'].'" else e end),(case when s < "'.$filter_info['sdate'].'" then "'.$filter_info['sdate'].'" else s end)) as t')->where('car_area',$filter_info['zone'])
            ->where('sign',1)
            ->where(['s'=>['>=',$now_time]])
            ->where(['caravan_id'=>['<>',$car_id]])
            ->order('t asc')
            ->limit(5)
            ->page($page)
            ->select();
        }
        //全部区域
        return $this->with('camps,caravans')->field('*,TIMESTAMPDIFF(DAY, (case when e > "'.$filter_info['edate'].'" then "'.$filter_info['edate'].'" else e end),(case when s < "'.$filter_info['sdate'].'" then "'.$filter_info['sdate'].'" else s end)) as t')
        ->where('sign',1)
        ->where(['s'=>['>=',$now_time]])
        ->where(['caravan_id'=>['<>',$car_id]])
        ->order('car_area asc')
        ->order('t asc')
        ->limit(5)
        ->page($page)
        ->select();
    }
    /**
     * [getList2 房车列表2其他城市推荐]
     * @param  [type] $filter_info [description]
     * @param  [type] $page        [description]
     * @return [type]              [description]
     */
    public function getList2($filter_info,$page)
    {
        //时间非空
        if (!isset($filter_info['sdate']) || !isset($filter_info['edate'])) {
            return false;
        }
        //当前时间资源开始时间需大于现在时间
        $now_time =date('Y-m-d');
        //剔除自己的房车
        $user_id = session('user.id');
        $car_id = model('CaravanInfos')->where('user_id',$user_id)->value('id');
        //地区排序升序
        if (!empty($filter_info['zone'])) {
            return $this->with('camps,caravans')->field('*,TIMESTAMPDIFF(DAY, (case when e > "'.$filter_info['edate'].'" then "'.$filter_info['edate'].'" else e end),(case when s < "'.$filter_info['sdate'].'" then "'.$filter_info['sdate'].'" else s end)) as t')
            ->where(['s'=>['>=',$now_time]])
            ->where('car_area','<>',$filter_info['zone'])
            ->where(['caravan_id'=>['<>',$car_id]])
            ->where('sign',1)
            ->order('car_area asc')
            ->limit(5)
            ->page($page)
            ->order('t asc')->select();
        }
        //全部地区
        return $this->with('camps,caravans')->field('*,TIMESTAMPDIFF(DAY, (case when e > "'.$filter_info['edate'].'" then "'.$filter_info['edate'].'" else e end),(case when s < "'.$filter_info['sdate'].'" then "'.$filter_info['sdate'].'" else s end)) as t')
            ->where('sign',1)
            ->where(['s'=>['>=',$now_time]])
            ->where(['caravan_id'=>['<>',$car_id]])
            ->order('car_area asc')
            ->limit(5)
            ->page($page)
            ->order('t asc')->select();
    }
    /**
     * [statistical 时间交集与差集统计]
     * @param  [type] $filter_info [条件集合]
     * @param  [type] $a           [判断>0为差集，<0为交集]->having('t',"$a",0)
     * @param  [type] $sort        [差集升序越来越远，交集越小交集越大]
     * @return [type]              [description]
     */
    // public function statistical($filter_info)
    // {   
    // return $list = $this->field('*, TIMESTAMPDIFF(DAY, (case when e > "'.$filter_info['edate'].'" then "'.$filter_info['edate'].'" else e end),(case when s < "'.$filter_info['sdate'].'" then "'.$filter_info['sdate'].'" else s end)) as t');
    // }
    /**
     * [FunctionName 城市列表数组]
     * @param string $value [description]
     */
    public function getCity()
    {
        $city = ['1'=>'Beijing','2'=>'Shanghai','3'=>'Sanya','4'=>'Chengdou','5'=>'Xian'];
        return $city;
    }
    /**
     * [getCityValue 城市转换]
     * @param  [type] $value [description]
     * @return [type]        [description]
     */
    public function getCityValue($value)
    {
        $city = ['北京'=>'1','上海'=>'2','三亚'=>'3','成都'=>'4','西安'=>'5','全部'=>''];
        return $city[$value];
    }
    /**
     * [getCates 类型转换]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getCates($id)
    {
        $grades = db('car_cates')->column('name', 'id');
        if (array_key_exists($id, $grades)) {
           return $grades[$id];
        }
        return '无';
    }
    public function getRadioName($value)
    {
        if (empty($value)) {
            return '无';
        }
        $status = ['1'=>'有','2'=>'无'];
        return $status[$value];
    }
    ///////////////////////////sign = 2////出租流程////////////////////////////////
    /**
     * [getList  房车列表3同一城市]
     * @param  [type] $filter_info [description]
     * @param  [type] $page        [description]
     * @return [type]              [description]
     */
    public function getList3($filter_info,$page)
    {
        //时间非空
        if (!isset($filter_info['sdate']) || !isset($filter_info['edate'])) {
            return false;
        }
        //当前时间资源开始时间需大于现在时间
        $now_time =date('Y-m-d');
        //剔除自己的房车
        $user_id = session('user.id');
        $car_id = model('CaravanInfos')->where('user_id',$user_id)->value('id') ? : '';
        //地点判断区域 登陆状态下
        if (!empty($filter_info['zone'])&&!empty($user_id)) {
            return $this->with('camps,caravans')->field('*,TIMESTAMPDIFF(DAY, (case when e > "'.$filter_info['edate'].'" then "'.$filter_info['edate'].'" else e end),(case when s < "'.$filter_info['sdate'].'" then "'.$filter_info['sdate'].'" else s end)) as t')->where('car_area',$filter_info['zone'])
            ->where('sign',2)
            ->where(['s'=>['>=',$now_time]])
            ->where(['caravan_id'=>['<>',$car_id]])
            ->order('t asc')
            ->limit(3)
            ->page($page)
            ->select();
        }
        //全部区域 登陆状态下
        if (!empty($user_id)) {
            return $this->with('camps,caravans')->field('*,TIMESTAMPDIFF(DAY, (case when e > "'.$filter_info['edate'].'" then "'.$filter_info['edate'].'" else e end),(case when s < "'.$filter_info['sdate'].'" then "'.$filter_info['sdate'].'" else s end)) as t')
                ->where('sign',2)
                ->where(['s'=>['>=',$now_time]])
                ->where(['caravan_id'=>['<>',$car_id]])
                ->order('car_area asc')
                ->order('t asc')
                ->limit(3)
                ->page($page)
                ->select();
        }
        //未登录状态下 地点判断区域
        if (!empty($filter_info['zone'])) {
            return $this->with('camps,caravans')->field('*,TIMESTAMPDIFF(DAY, (case when e > "'.$filter_info['edate'].'" then "'.$filter_info['edate'].'" else e end),(case when s < "'.$filter_info['sdate'].'" then "'.$filter_info['sdate'].'" else s end)) as t')->where('car_area',$filter_info['zone'])
            ->where('sign',2)
            ->where(['s'=>['>=',$now_time]])
            ->order('t asc')
            ->limit(3)
            ->page($page)
            ->select();
        }
        //未登录状态下
        return $this->with('camps,caravans')->field('*,TIMESTAMPDIFF(DAY, (case when e > "'.$filter_info['edate'].'" then "'.$filter_info['edate'].'" else e end),(case when s < "'.$filter_info['sdate'].'" then "'.$filter_info['sdate'].'" else s end)) as t')
            ->where('sign',2)
            ->where(['s'=>['>=',$now_time]])
            ->order('car_area asc')
            ->order('t asc')
            ->limit(3)
            ->page($page)
            ->select();
        
    }
    /**
     * [getList4 房车列表4其他城市推荐]
     * @param  [type] $filter_info [description]
     * @param  [type] $page        [description]
     * @return [type]              [description]
     */
    public function getList4($filter_info,$page)
    {
        //时间非空
        if (!isset($filter_info['sdate']) || !isset($filter_info['edate'])) {
            return false;
        }
        //当前时间资源开始时间需大于现在时间
        $now_time =date('Y-m-d');
        //剔除自己的房车
        $user_id = session('user.id');
        $car_id = model('CaravanInfos')->where('user_id',$user_id)->value('id') ? : '';
        //地区排序升序登陆情况下
        if (!empty($filter_info['zone'])&&!empty($user_id)) {
            return $this->with('camps,caravans')->field('*,TIMESTAMPDIFF(DAY, (case when e > "'.$filter_info['edate'].'" then "'.$filter_info['edate'].'" else e end),(case when s < "'.$filter_info['sdate'].'" then "'.$filter_info['sdate'].'" else s end)) as t')
            ->where(['s'=>['>=',$now_time]])
            ->where('car_area','<>',$filter_info['zone'])
            ->where(['caravan_id'=>['<>',$car_id]])
            ->where('sign',2)
            ->order('car_area asc')
            ->limit(3)
            ->page($page)
            ->order('t asc')->select();
        }
        //全部地区 登陆情况下
        if (!empty($user_id)) {
           return $this->with('camps,caravans')->field('*,TIMESTAMPDIFF(DAY, (case when e > "'.$filter_info['edate'].'" then "'.$filter_info['edate'].'" else e end),(case when s < "'.$filter_info['sdate'].'" then "'.$filter_info['sdate'].'" else s end)) as t')
            ->where('sign',2)
            ->where(['s'=>['>=',$now_time]])
            ->where(['caravan_id'=>['<>',$car_id]])
            ->order('car_area asc')
            ->limit(3)
            ->page($page)
            ->order('t asc')->select();
        }
        //未登录情况下 地区排序升序
         if (!empty($filter_info['zone'])) {
            return $this->with('camps,caravans')->field('*,TIMESTAMPDIFF(DAY, (case when e > "'.$filter_info['edate'].'" then "'.$filter_info['edate'].'" else e end),(case when s < "'.$filter_info['sdate'].'" then "'.$filter_info['sdate'].'" else s end)) as t')
            ->where(['s'=>['>=',$now_time]])
            ->where('car_area','<>',$filter_info['zone'])
            ->where('sign',2)
            ->order('car_area asc')
            ->limit(3)
            ->page($page)
            ->order('t asc')->select();
        }
        //未登录情况下
        return $this->with('camps,caravans')->field('*,TIMESTAMPDIFF(DAY, (case when e > "'.$filter_info['edate'].'" then "'.$filter_info['edate'].'" else e end),(case when s < "'.$filter_info['sdate'].'" then "'.$filter_info['sdate'].'" else s end)) as t')
            ->where('sign',2)
            ->where(['s'=>['>=',$now_time]])
            ->order('car_area asc')
            ->limit(3)
            ->page($page)
            ->order('t asc')->select();
    }


}